package com.yinhe.music.yhmusic.personal.vip;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.common.GlideHelper.GlideHelper;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.UserInfo;
import com.yinhe.music.yhmusic.personal.adapter.VipListAdapter;
import com.yinhe.music.yhmusic.personal.model.VipListInfo;
import com.yinhe.music.yhmusic.service.PlayService;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.yinhe.music.yhmusic.widget.DividerItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;

/**
 * @author hasee
 * @date 2017/7/10
 */

public class MyVipActivity extends BaseServiceActivity implements ProductListContract.IProductListView {
    @BindView(R.id.my_vip_layout)
    RelativeLayout myVipLayout;
    private TextView days, songNum, name, limit;
    private ImageView image;
    private LinearLayout no_vip, vip;
    private RecyclerView mRecyclerView;
    private VipListAdapter mAdapter;

    ProductPresenter mPresenter;

    @Override
    public void onServiceConnected(PlayService service) {
        super.onServiceConnected(service);
        setVipInfo();
        mPresenter.getProductList(IConstants.PRODUCT_MEMBER);
    }

    @Override
    public void initView() {
        RxBus.get().register(this);
        days = findViewById(R.id.days);
        songNum = findViewById(R.id.song_num);
        name = findViewById(R.id.tv_nickname);
        image = findViewById(R.id.image);
        no_vip = findViewById(R.id.no_vip_layout);
        vip = findViewById(R.id.vip_layout);
        mRecyclerView = findViewById(R.id.recyclerView);
        limit = findViewById(R.id.limit);

        setToolbar("我的会员");
        setmRecyclerView();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_my_vip;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new ProductPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    private void setmRecyclerView() {
        mAdapter = new VipListAdapter(null, this);
        WrapContentLinearLayoutManager layoutManager = new WrapContentLinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST, 0, true));
    }

    private void setVipInfo() {
        if (!Preferences.getLoginSn().isEmpty()) {
            mPresenter.getUserInfo();
        } else {
            no_vip.setVisibility(View.VISIBLE);
            vip.setVisibility(View.GONE);
        }
    }

    @Override
    public void setProductListUI(VipListInfo list) {
        List<VipListInfo> vipList = new ArrayList<>(list.getMemberInfo());
        mAdapter.update(vipList);
    }

    @Override
    public void setUserUI(UserInfo info) {
        GlideHelper.setCircleImageResource(image, info.getImage());

        if (info.getNickname() != null) {
            name.setText(info.getNickname());
        } else {
            name.setText(" ");
        }

        if (info.getVipLevel() == 0) {
            no_vip.setVisibility(View.VISIBLE);
            vip.setVisibility(View.GONE);
        } else {
            no_vip.setVisibility(View.GONE);
            vip.setVisibility(View.VISIBLE);
            days.setText(String.valueOf(info.getVipExpire()));
            songNum.setText(String.format(Locale.CHINESE, "%d/%d", info.getVipPurchaseSong(), info.getVipPurchaseSongLimit()));
        }
        limit.setText(String.format(Locale.CHINESE, "每月%d首的下载音乐配额", info.getVipPurchaseSongLimit()));
    }


    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Pay.PAY_STATUS)
    )
    public void onRxbusPaystatus(Integer payStatus) {
        LogUtils.i(payStatus);
        mPresenter.getUserInfo();
    }
}
